"use strict";

var _interopRequireDefault = require("@babel/runtime/helpers/interopRequireDefault").default;

Object.defineProperty(exports, "__esModule", {
  value: true
});
exports.default = void 0;

var _objectSpread2 = _interopRequireDefault(require("@babel/runtime/helpers/objectSpread2"));

var _jsxRuntime = require("react/jsx-runtime");

var _proProvider = require("@ant-design/pro-provider");

var _proUtils = require("@ant-design/pro-utils");

var _antdV = require("antd-v4");

var _moment = _interopRequireDefault(require("moment"));

/**
 * 与当前的时间进行比较 http://momentjs.cn/docs/displaying/fromnow.html
 *
 * @param
 */
var FieldFromNow = function FieldFromNow(_ref) {
  var text = _ref.text,
      mode = _ref.mode,
      render = _ref.render,
      renderFormItem = _ref.renderFormItem,
      format = _ref.format,
      fieldProps = _ref.fieldProps;
  var intl = (0, _proProvider.useIntl)();

  if (mode === 'read') {
    var dom = (0, _jsxRuntime.jsx)(_antdV.Tooltip, {
      title: (0, _moment.default)(text).format((fieldProps === null || fieldProps === void 0 ? void 0 : fieldProps.format) || format || 'YYYY-MM-DD HH:mm:ss'),
      children: (0, _moment.default)(text).fromNow()
    });

    if (render) {
      return render(text, (0, _objectSpread2.default)({
        mode: mode
      }, fieldProps), (0, _jsxRuntime.jsx)(_jsxRuntime.Fragment, {
        children: dom
      }));
    }

    return (0, _jsxRuntime.jsx)(_jsxRuntime.Fragment, {
      children: dom
    });
  }

  if (mode === 'edit' || mode === 'update') {
    var placeholder = intl.getMessage('tableForm.selectPlaceholder', '请选择');
    var momentValue = (0, _proUtils.parseValueToMoment)(fieldProps.value);

    var _dom = (0, _jsxRuntime.jsx)(_antdV.DatePicker, (0, _objectSpread2.default)((0, _objectSpread2.default)({
      placeholder: placeholder,
      showTime: true
    }, fieldProps), {}, {
      value: momentValue
    }));

    if (renderFormItem) {
      return renderFormItem(text, (0, _objectSpread2.default)({
        mode: mode
      }, fieldProps), _dom);
    }

    return _dom;
  }

  return null;
};

var _default = FieldFromNow;
exports.default = _default;